﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.BLL;
using Wsitech.Model;

public partial class NewsCenter_Search : System.Web.UI.Page
{
    protected static readonly int pagesize = 10;
    protected readonly string param = HttpContext.Current.Request["param"] ?? "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (param != "")
            {
                txtParam.Text = param;
                bindSearchResult();
            }
        }
    }

    private void bindSearchResult()
    {
        int count = 0;
        if (txtParam.Text.Trim() != "")
        {
            IList<TextInfo> list = TextInfoBLL.Instance.SearchInfo(TextInfoBLL.EN_NEWS_ID, txtParam.Text.Trim(), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, ref count);
            if (list.Count > 0)
            {
                repMain.DataSource = list;
                repMain.DataBind();
                lbResult.Text = string.Format("与\"{0}\"相关的{1}条结果", param, count);
            }
            else
            {
                repMain.DataSource = TextInfoBLL.Instance.GetActivePagedListByParentSiteMapID(TextInfoBLL.EN_NEWS_ID, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, ref count);
                repMain.DataBind();
                lbResult.Text = string.Format("未找到与\"{0}\"相关的结果，以下为您显示最新发布的信息！", param);
            }

            AspNetPager1.RecordCount = count;
            AspNetPager2.RecordCount = count;
        }
        else
        {
            lbResult.Text = "搜索条件不能为空！";
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("{0}?param={1}", Request.Path, txtParam.Text.Trim()));
    }

    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {
        bindSearchResult();
    }

    protected void repMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        TextInfo info = e.Item.DataItem as TextInfo;
        //标题链接
        HyperLink hlTitle = (HyperLink)e.Item.FindControl("hlTitle");
        //显示内容简介
        Label lbDescription = (Label)e.Item.FindControl("lbDescription");
        if (info != null)
        {
            //去除HTML
            string content = TextInfoBLL.Instance.RemoveHTML(info.Content);

            lbDescription.Text = TextInfoBLL.Instance.CutString(content, 240, "...");

            string url = "";

            if (info.SitemapId == TextInfoBLL.SITEMAPID_PRESS_RELEASE)
            {
                url = Wsitech.Utility.GlobalConst.NEWS_VIEW_EN;
            }
            else if (info.SitemapId == TextInfoBLL.SITEMAPID_MEDIA_REPORT)
            {
                url = Wsitech.Utility.GlobalConst.REPORT_VIEW_EN;
            }

            hlTitle.NavigateUrl = url + "?tid=" + info.TextInfoId;
        }



    }
}